package examples.camera.component
{
	import flash.display.BitmapData;
	import flash.events.ActivityEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Camera;
	import flash.media.Video;

	import mx.core.UIComponent;
	import mx.graphics.ImageSnapshot;

	import spark.components.Button;

	public class CameraUI extends UIComponent
	{
		private var video:Video;
		private var button:Button;
		private var camera:Camera;

		public function CameraUI()
		{
			super();
			camera = Camera.getCamera();

			if (camera != null) {
				video = new Video(camera.width * 2, camera.height * 2);
				video.attachCamera(camera);
				addChild(video);
			}
		}

		protected override function createChildren():void {
			button = new Button();
			button.label = "撮影";
			button.width = camera.width * 2;
			button.height = 20;
			button.addEventListener(MouseEvent.CLICK, onShot);
			addChild(button);
		}

		protected override function measure():void {
			super.measure();
			measuredMinWidth = measuredWidth = camera.width * 2;
			measuredHeight = measuredHeight = (camera.height * 2) + 10 + button.height;
			button.move(0, (camera.height * 2) + 10);
		}

		private function onShot(event:MouseEvent):void {
			sendMessage(this, "shot", ImageSnapshot.captureBitmapData(video));
		}
	}
}